﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// Qt headers
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>

// custom headers
#include "BaseDialog.h"
#include "TitleBar.h"
#include "GbmpNew.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 
namespace gmepgcs
{
    BaseDialog::BaseDialog(QWidget * parent, Qt::WindowFlags flags)
        : QDialog(parent, flags)
    {
        Init();
    }

    BaseDialog::~BaseDialog()
    {
    }

    void BaseDialog::SetCentralWidget(QWidget *pWidget)
    {
        QLayoutItem *pChildItem;
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHCentralLayout, L"m_pHCentralLayout为空", L"GMEP", L"2024-01-30");
        while ( (pChildItem = m_pHCentralLayout->takeAt(0)) != nullptr)
        {
            if (pChildItem->widget())
            {
                pChildItem->widget()->setParent(nullptr);
            }
            delete pChildItem;
        }

        if (pWidget)
        {
            m_pHCentralLayout->addWidget(pWidget);
        }
    }

    void BaseDialog::AddBottomButton(QPushButton * pButton)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHBottomButtonsLayout, L"m_pHBottomButtonsLayout为空", L"GMEP", L"2024-01-30");
        m_pHBottomButtonsLayout->addWidget(pButton);
    }

    void BaseDialog::AddBottomStretch(int stretch/* = 0*/)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHBottomButtonsLayout, L"m_pHBottomButtonsLayout为空", L"GMEP", L"2024-01-30");
        m_pHBottomButtonsLayout->addStretch(stretch);
    }

    void BaseDialog::PushFrontBottomButton(QPushButton * pButton)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHBottomButtonsLayout, L"m_pHBottomButtonsLayout为空", L"GMEP", L"2024-01-30");
        m_pHBottomButtonsLayout->insertWidget(0, pButton);
    }

    void BaseDialog::SetMinBoxVisible(bool visible)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pTitleBar, L"m_pTitleBar为空", L"GMEP", L"2024-01-30");
        m_pTitleBar->SetMinBoxVisible(visible);
    }

    void BaseDialog::SetMaxBoxVisible(bool visible)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pTitleBar, L"m_pTitleBar为空", L"GMEP", L"2024-01-30");
        m_pTitleBar->SetMaxBoxVisible(visible);
    }

    void BaseDialog::SetCloseBoxVisible(bool visible)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pTitleBar, L"m_pTitleBar为空", L"GMEP", L"2024-01-30");
        m_pTitleBar->SetCloseBoxVisible(visible);
    }

    void BaseDialog::SetCentralWidgetMargin(const QMargins & margins)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHCentralLayout, L"m_pHCentralLayout为空", L"GMEP", L"2024-01-30");
        m_pHCentralLayout->setContentsMargins(margins);
    }

    bool BaseDialog::eventFilter(QObject * obj, QEvent * event)
    {
        return false;
    }

    void BaseDialog::paintEvent(QPaintEvent * event)
    {
    }

    void BaseDialog::Init()
    {
        // set title bar
        setWindowFlags(Qt::FramelessWindowHint | windowFlags());
        m_pTitleBar = NEW_AS_QT(TitleBar, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pTitleBar, L"m_pTitleBar为空", L"GMEP", L"2024-01-30");
        installEventFilter(m_pTitleBar);
        QPalette palette = m_pTitleBar->palette();
        palette.setColor(QPalette::Background, QColor(176, 178, 185, 255));
        m_pTitleBar->setAutoFillBackground(true);
        m_pTitleBar->setPalette(palette);

        // set central layout
        m_pHCentralLayout = NEW_AS_QT(QHBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHCentralLayout, L"m_pHCentralLayout为空", L"GMEP", L"2024-01-30");
        m_pHCentralLayout->setSpacing(0);
        m_pHCentralLayout->setContentsMargins(0, 0, 0, 0);
        
        // set bottom buttons layout
        m_pHBottomButtonsLayout = NEW_AS_QT(QHBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHBottomButtonsLayout, L"m_pHBottomButtonsLayout为空", L"GMEP", L"2024-01-30");
        m_pHBottomButtonsLayout->setSpacing(8);
        m_pHBottomButtonsLayout->setContentsMargins(16, 8, 16, 16);
        m_pHBottomButtonsLayout->setSizeConstraint(QLayout::SizeConstraint::SetMinimumSize);

        // set main layout
        QVBoxLayout *pVMainLayout = NEW_AS_QT_CHILD(QVBoxLayout, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pVMainLayout, L"pVMainLayout为空", L"GMEP", L"2024-01-30");
        pVMainLayout->setSpacing(0);
        pVMainLayout->setContentsMargins(1, 1, 1, 1);
        pVMainLayout->addWidget(m_pTitleBar);
        pVMainLayout->addLayout(m_pHCentralLayout);
        pVMainLayout->addLayout(m_pHBottomButtonsLayout);
        pVMainLayout->setAlignment(m_pTitleBar, Qt::AlignTop);
        pVMainLayout->setAlignment(m_pHBottomButtonsLayout, Qt::AlignBottom);

        // set BaseDialog self
        setLayout(pVMainLayout);
        setMinimumSize(248, 128);
        setWindowTitle(QString());
        setStyleSheet("QDialog{"
            "background-color:rgba(245,245,245,255);"
            "border-radius:2px;"
            "border:1px solid rgba(189,199,213,255);"
            "}"
        );
    }
}